$(function(){
    $("#button_click").on('click',function(index,item){
        var channelId = 5;
        var title = $("#title").val();
        var openInfo = $("input[name='openInfo']:checked").val();
        var content = $("#content").val();

        var pictureList = '';
        $(".schoolPics").each(function(index,item){
            pictureList+=$(this).val()+"#";
        });

        $.ajax({
            url : "/post/ajaxSubmit",
            type : "POST",
            data : {title:title,openInfo:openInfo,channelId:channelId,content:content,pictureList:pictureList},
            dataType : "json",
            success : function(data) {
                layer.msg('修改成功，跳转中...', {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    // window.location.href = "/user/profile";
                });
            }
        });
    });
});

function loadDelIcon(){
    $('.up_del_icon').on('click', function(){
        $(this).parent('.up_item').remove();
    });

    var pictureList = '';
    $(".schoolPics").each(function(index,item){
        pictureList+=$(this).val()+"#";
    });
    $("#pictureList").val(pictureList);


}

function loadInitUpload(){
    var layerIndex;
    var uploader = Qiniu.uploader({
        runtimes: 'html5,flash,html4',
        browse_button: 'pickfiles',
        container: 'container',//父节点id
        filters: {
            mime_types : [ //只允许上传图片和zip文件
                { title : "Image files", extensions : "jpg,gif,png,jpeg" }
                //         // { title : "Zip files", extensions : "zip" }
            ],
            max_file_size : '10m', //最大只能上传文件大小
            //     prevent_duplicates : true //不允许选取重复文件
        },
        // extensions:'jpg,gif,png,jpeg',
        max_file_size: '10mb',//上传文件最大大小
        flash_swf_url: 'js/plupload/Moxie.swf',
        dragdrop: true,
        chunk_size: '10mb',
        unique_names: true,
        uptoken_url: "/getQiNiuToken",//如果页面没有相关节点则需要在这里写值
        domain: "http://pbl71lr73.bkt.clouddn.com/",//如果页面没有相关节点则需要在这里写值
        auto_start: true,
        init: {
            'FilesAdded': function (up, files) {
                plupload.each(files, function (file) {
                    console.log("等待。。。。。")
                });
            },
            'BeforeUpload': function (up, file) {
                layerIndex = layer.load('图片正在上传......',{icon:3,shade:0.6});
                console.log("文件开始上传。。。。");
            },
            'UploadProgress': function (up, file) {
                console.log("文件上传中。。。。");
            },
            'UploadComplete': function () {
                console.log("文件全部上传完成。。。...");
                layer.close(layerIndex);
            },
            'FileUploaded': function (up, file, info) {
                var dataset = $.parseJSON(info.response);
                var resonseUrl = dataset.key;
                var strHtml = "<span class='up_item'>" +
                    "<img data-image='"+resonseUrl+"' src='http://pbl71lr73.bkt.clouddn.com/"+resonseUrl+"?imageView2/1/w/270/h/162' alt='' style='width:90px;height:90px;'/> " +
                    "<input type='hidden' value='"+resonseUrl+"' name='schoolPics' class='schoolPics'/> " +
                    // "<a target='_blank' href='http://pbl71lr73.bkt.clouddn.com/"+resonseUrl+"'>" +
                    // "<i class='enlarge_icon'></i>" +
                    // "</a> " +
                    "<i class='up_del_icon'></i> " +
                    "</span>";
                $("#container").before(strHtml);
                layer.close(layerIndex);
                loadDelIcon();
            },
            'Error': function (up, err, errTip) {
                layer.msg('文件上传失败'+errTip);
                console.log("文件上传失败", err, errTip);
                layer.close(layerIndex);
            }
        }
    });
}